﻿using SD.Toolkits.Mapper;
using SlamDunk.MES.Domain.Entities.DefinitionContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.DefinitionContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.FoundationContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.ResourceContext;
using System;
using System.Collections.Generic;

namespace SlamDunk.MES.AppService.Maps
{
    /// <summary>
    /// 产品定义相关映射
    /// </summary>
    public static class DefinitionMap
    {
        #region # 物料清单映射 —— static BomInfo ToDTO(this Bom bom...
        /// <summary>
        /// 物料清单映射
        /// </summary>
        public static BomInfo ToDTO(this Bom bom, IDictionary<Guid, MaterialInfo> productInfos, IDictionary<Guid, FacilityInfo> facilityInfos)
        {
            BomInfo bomInfo = bom.Map<Bom, BomInfo>();
            bomInfo.ProductInfo = productInfos?[bom.ProductId];
            bomInfo.FacilityInfo = bom.FacilityId.HasValue
                ? facilityInfos?[bom.FacilityId.Value]
                : null;

            return bomInfo;
        }
        #endregion

        #region # 物料清单项映射 —— static BomItemInfo ToDTO(this BomItem bomItem...
        /// <summary>
        /// 物料清单项映射
        /// </summary>
        public static BomItemInfo ToDTO(this BomItem bomItem, IDictionary<Guid, MaterialInfo> materialInfos)
        {
            BomItemInfo bomItemInfo = bomItem.Map<BomItem, BomItemInfo>();
            bomItemInfo.MaterialInfo = materialInfos?[bomItem.MaterialId];

            return bomItemInfo;
        }
        #endregion

        #region # 工艺类映射 —— static ProcessClassInfo ToDTO(this ProcessClass processClass...
        /// <summary>
        /// 工艺类映射
        /// </summary>
        public static ProcessClassInfo ToDTO(this ProcessClass processClass, IDictionary<Guid, FacilityInfo> facilityInfos)
        {
            ProcessClassInfo processClassInfo = processClass.Map<ProcessClass, ProcessClassInfo>();
            processClassInfo.FacilityInfo = processClass.FacilityId.HasValue
                ? facilityInfos?[processClass.FacilityId.Value]
                : null;

            return processClassInfo;
        }
        #endregion

        #region # 工艺映射 —— static ProcessInfo ToDTO(this Process process...
        /// <summary>
        /// 工艺映射
        /// </summary>
        public static ProcessInfo ToDTO(this Process process, IDictionary<Guid, ProcessClassInfo> processClassInfos, IDictionary<Guid, MaterialInfo> productInfos, IDictionary<Guid, FacilityInfo> facilityInfos)
        {
            ProcessInfo processInfo = process.Map<Process, ProcessInfo>();
            processInfo.ProcessClassInfo = processClassInfos?[process.ProcessClassId];
            processInfo.ProductInfo = process.ProductId.HasValue
                ? productInfos?[process.ProductId.Value]
                : null;
            processInfo.FacilityInfo = process.FacilityId.HasValue
                ? facilityInfos?[process.FacilityId.Value]
                : null;

            return processInfo;
        }
        #endregion

        #region # 工艺段映射 —— static ProcessSegmentInfo ToDTO(this ProcessSegment segment...
        /// <summary>
        /// 工艺段映射
        /// </summary>
        public static ProcessSegmentInfo ToDTO(this ProcessSegment segment, IDictionary<Guid, FacilityInfo> facilityInfos, IDictionary<Guid, WorkStationInfo> workStationInfos)
        {
            ProcessSegmentInfo segmentInfo = segment.Map<ProcessSegment, ProcessSegmentInfo>();
            segmentInfo.ProcessId = segment.Process.Id;
            segmentInfo.FacilityInfo = segment.FacilityId.HasValue
                ? facilityInfos?[segment.FacilityId.Value]
                : null;
            segmentInfo.WorkStationInfo = segment.WorkStationId.HasValue
                ? workStationInfos?[segment.WorkStationId.Value]
                : null;

            return segmentInfo;
        }
        #endregion

        #region # 工艺段人员规范映射 —— static SegmentPersonRuleInfo ToDTO(this SegmentPersonRule...
        /// <summary>
        /// 工艺段人员规范映射
        /// </summary>
        public static SegmentPersonRuleInfo ToDTO(this SegmentPersonRule personRule, IDictionary<Guid, PersonClassInfo> personClassInfos, IDictionary<Guid, PersonInfo> personInfos, IDictionary<Guid, FacilityInfo> facilityInfos)
        {
            SegmentPersonRuleInfo personRuleInfo = personRule.Map<SegmentPersonRule, SegmentPersonRuleInfo>();
            personRuleInfo.PersonClassInfo = personRule.PersonClassId.HasValue
                ? personClassInfos?[personRule.PersonClassId.Value]
                : null;
            personRuleInfo.PersonInfo = personRule.PersonId.HasValue
                ? personInfos?[personRule.PersonId.Value]
                : null;
            personRuleInfo.FacilityInfo = personRule.FacilityId.HasValue
                ? facilityInfos?[personRule.FacilityId.Value]
                : null;

            return personRuleInfo;
        }
        #endregion

        #region # 工艺段设备规范映射 —— static SegmentEquipmentRuleInfo ToDTO(this SegmentEquipmentRule...
        /// <summary>
        /// 工艺段设备规范映射
        /// </summary>
        public static SegmentEquipmentRuleInfo ToDTO(this SegmentEquipmentRule equipmentRule, IDictionary<Guid, EquipmentClassInfo> equipmentClassInfos, IDictionary<Guid, EquipmentInfo> equipmentInfos, IDictionary<Guid, FacilityInfo> facilityInfos)
        {
            SegmentEquipmentRuleInfo equipmentRuleInfo = equipmentRule.Map<SegmentEquipmentRule, SegmentEquipmentRuleInfo>();
            equipmentRuleInfo.EquipmentClassInfo = equipmentRule.EquipmentClassId.HasValue
                ? equipmentClassInfos?[equipmentRule.EquipmentClassId.Value]
                : null;
            equipmentRuleInfo.EquipmentInfo = equipmentRule.EquipmentId.HasValue
                ? equipmentInfos?[equipmentRule.EquipmentId.Value]
                : null;
            equipmentRuleInfo.FacilityInfo = equipmentRule.FacilityId.HasValue
                ? facilityInfos?[equipmentRule.FacilityId.Value]
                : null;

            return equipmentRuleInfo;
        }
        #endregion

        #region # 工艺段物料规范映射 —— static SegmentMaterialRuleInfo ToDTO(this SegmentMaterialRule...
        /// <summary>
        /// 工艺段物料规范映射
        /// </summary>
        public static SegmentMaterialRuleInfo ToDTO(this SegmentMaterialRule materialRule, IDictionary<Guid, MaterialInfo> materialInfos, IDictionary<Guid, FacilityInfo> facilityInfos)
        {
            SegmentMaterialRuleInfo materialRuleInfo = materialRule.Map<SegmentMaterialRule, SegmentMaterialRuleInfo>();
            materialRuleInfo.MaterialInfo = materialInfos?[materialRule.MaterialId];
            materialRuleInfo.FacilityInfo = materialRule.FacilityId.HasValue
                ? facilityInfos?[materialRule.FacilityId.Value]
                : null;

            return materialRuleInfo;
        }
        #endregion

        #region # 工艺段参数规范映射 —— static SegmentParameterRuleInfo ToDTO(this SegmentParameterRule...
        /// <summary>
        /// 工艺段参数规范映射
        /// </summary>
        public static SegmentParameterRuleInfo ToDTO(this SegmentParameterRule parameterRule, IDictionary<Guid, FacilityInfo> facilityInfos)
        {
            SegmentParameterRuleInfo parameterRuleInfo = parameterRule.Map<SegmentParameterRule, SegmentParameterRuleInfo>();
            parameterRuleInfo.FacilityInfo = parameterRule.FacilityId.HasValue
                ? facilityInfos?[parameterRule.FacilityId.Value]
                : null;

            return parameterRuleInfo;
        }
        #endregion
    }
}
